var forexCRT_SettingPanel=function(callback,cfg){
	this.lg = QS_Lang.forexCRT;
    this.html="<div class='forex-crt-settingpanel' style='width:148px;'><div style='clear:both'><h3>"+this.lg["forexs"]+"</h3><div class='all-forexs-wrap'><div class='all-forexs'></div></div></div><div class='forex-maxcount-tip'></div><a class='qs-ui-btn apply'>"+this.lg["apply"]+"</a><h3 class='rate-type-title'>"+this.lg["rate"]+"</h3><div class='rate-type'></div><h3>"+this.lg["monitor"]+"</h3><div class='monitor-type'></div><h3 class='forex-date-title'>"+this.lg["date"]+"</h3><div class='forex-date'><input type='text' class='qs-ui-ipt'/></div></div>";
    this.forexs=["USD","EUR","JPY","GBP","CHF","AUD","CAD","SEK","HKD","NOK","NZD","MXN","SGD","CNY","INR","PKR","SAR","ZAR","ALL","ARE","ARS","BRL","AED","CZK","DKK","FJD","HUF","IDR","ILS","PLN","MYR","THB","TWD"];
    this.scrollPanel=null;
    this.cfg = $.extend(true,{maxCountTip:"Note: Max 8 currencies",count:-1,monitor:"BidPrice",rate:"spot",history:false,rateAble:true,forwardList:[{"txt":this.lg["spot"],"val":"spot"},{"txt":this.lg["forward1week"],"val":"262"},{"txt":this.lg["forward1month"],"val":"268"},{"txt":this.lg["forward3maonth"],"val":"272"},{"txt":this.lg["forward6month"],"val":"278"},{"txt":this.lg["forward1year"],"val":"282"}]},cfg);
    this.checkedList=null;
    this.checkedMap={};
    this.el = null;
    this.checkListButton=null;
    this.ddl_monitor = null;
    this.callback=$.extend(true,{changeForexs:function(){},changeMonitor:function(){}},callback);
};
forexCRT_SettingPanel.prototype={
	parseCfg:function(cfg){
		$.extend(this.cfg,cfg);
		this.checkedList = this.cfg.checkedList;
		this._init();
	},
    _init:function(){
        var self = this;
        this.el = $(this.html);
		if(this.cfg.history==false){
			this.el.find(".forex-date").hide();
			this.el.find(".forex-date-title").hide();
		}
		if(this.cfg.rateAble==false){
			this.el.find(".rate-type").hide();
            this.el.find(".rate-type-title").hide();
		}
        this.initCheckBoxs(this.checkedList);       
        this._bindEvent();
		if(this.cfg.count>-1){
			this.el.find(".forex-maxcount-tip").html(this.cfg.maxCountTip);
		}
        if(self.scrollPanel==null){
            self.scrollPanel = new Util.ScrollPanel(self.el.find('.all-forexs'),{});
        }
        this.ddl_monitor=new Util.DropDownList(self.el.find('.monitor-type')[0], function(t,v){
            self.callback.changeMonitor(v);       
        }, [{"txt":this.lg["bidpriece"],"val":"BidPrice"},{"txt":this.lg["crtaskprice"],"val":"AskPrice"}], {triggerByClick:true, width:148,disable:false,actionAtInit:false});
        this.ddl_monitor.setTextValue(null,this.cfg.monitor);
        this.ddl_rate=new Util.DropDownList(self.el.find('.rate-type')[0], function(t,v){
            self.callback.changeRate(v);       
        }, this.cfg.forwardList, {triggerByClick:true, width:148,disable:false,actionAtInit:false});
        this.ddl_rate.setTextValue(null,this.cfg.rate);
        if(this.cfg.history==true){
			  QSCAL.register(self.el.find(".forex-date input")[0],function(date){
            var d = RTQ_ChartUtil.formatDate(date);
             self.el.find(".forex-date input").val(d); 
             self.callback.changeDate(date);  
         });
         self.el.find(".forex-date input").focus(function(e){
                var target=$(e.target);
                if ($.browser.msie) {       // fixed the issue that hit backspace key will back the web page.
                    document.body.focus();
                }
                QSCAL.show(target);
            });
		}
    },
    onOpenSettingPN:function(){
        if (this.scrollPanel) {
            //this.scrollPanel.reSize(147, 97);
			this.scrollPanel.reSize(null,60);
        }
    },
    initCheckBoxs:function(forexsList){
        if(!forexsList){
            return;
        }
        var self =this;
        var chkList=[];
        this.el.find('.all-forexs').html("");
        for(var i =0;i<forexsList.length;i++){
            if($.inArray(forexsList[i],this.forexs)==-1){
                this.forexs.push(forexsList[i]);
            }
        }
        for(var i =0;i<this.forexs.length;i++){
            chkList.push({txt:this.forexs[i],val:this.forexs[i]});
        }
        this.checkListButton = new Util.ButtonGroup(this.el.find('.all-forexs'), chkList, {
            onClick: function(v, b){
                self.checkedMap[v]= b.isSelected;
				self._changeCheckBoxStatus();
            }
        }, {
            type: 'checkbox',
            vertical: false
        });
        this.checkedMap={};
        this.checkedList = forexsList
        var checkedList =this.checkedList;
        for(var i=0;i<checkedList.length;i++){
            this.checkedMap[checkedList[i]] = true;
            this.checkListButton.btns[checkedList[i]].select();
        }
		this._changeCheckBoxStatus();
    },
	_changeCheckBoxStatus:function(){
		if(this.cfg.count<0){
			return;
		}
		var btns = this.checkListButton.btns;
		var count = 0;
		for(var i in btns){
			if(btns[i].isSelected){
				count ++;
			}
			btns[i].disable(false);
		}
		if(count>=this.cfg.count){
			for(var i in btns){
				if(!btns[i].isSelected){
				btns[i].disable(true);
				}
			}
		}

	},
    _bindEvent:function(){
        var self = this;
        this.el.find(".apply").bind("click",function(){
            var list = [],t=null;
            for(var i =0;i<self.forexs.length;i++){
                t = self.forexs[i];
                if(self.checkedMap.hasOwnProperty(t)&&self.checkedMap[t]){
                    list.push(t);
                }
            }
            if(list.length==0){
                Util.message("You haven't selected any forexs.");
                return;
            }else if(list.length==1){
                Util.message("Please select more than one forex.");
                return;
            }
            self.callback.changeForexs(list);
        })
    },
    getDom:function(){
        return this.el;
    }
};